package com.singleton.type2;

/**
 * @author 薛虎
 * @date 2020/12/15 14:11
 */
public class SingletonTest02 {

    public static void main(String[] args) {
        //测试
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance == instance1);
    }

}


//饿汉式(静态代码块)
class Singleton {

    //2.本类内部创建对象实例
    private static Singleton instance;

    static { //静态代码块中,创建单例对象
        instance = new Singleton();
    }

    //1.构造器私有化,外部能new
    private Singleton() {

    }

    //3.提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance() {
        return instance;
    }


}
